package com.example.mq.rabbit;

import com.rabbitmq.client.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("xrb");
        factory.setPassword("xrb");
        //设置RabbitMQ地址
        factory.setHost("192.168.116.130");
        factory.setVirtualHost("/");
        //建立到代理服务器的连接
        Connection conn = factory.newConnection();
        //创建信道
        Channel channel = conn.createChannel();
        //声明交换器
        String exchangeName = "hello-exchange";
        //参数1：交换器名称，参数2：交换器类型，参数3：交换器是否持久话
        channel.exchangeDeclare(exchangeName,"direct",true);
        String routingKey = "testRoutinKey";
        //发布消息
        byte[] messageBodyBytes = "quit".getBytes();
        channel.basicPublish(exchangeName,routingKey,null,messageBodyBytes);
        //关闭信道和连接
        channel.close();
        conn.close();
    }
}
